package cn.kgc.tangcco.tcbd1011.books.action;

import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.kgc.tangcco.tcbd1011.books.entity.Books;
import cn.kgc.tangcco.tcbd1011.books.service.BooksService;
import cn.kgc.tangcco.tcbd1011.books.vo.BooksVo;
import cn.kgc.tangcco.tcbd1011.comm.base.BaseServlet;
import cn.kgc.tangcco.tcbd1011.comm.properties.PropertiesFactory;
import cn.kgc.tangcco.tcbd1011.util.MyBeanUtils;
import cn.kgc.tangcco.tcbd1011.util.Util;

@WebServlet(name="BooksServlet",urlPatterns="/books.action")
public class BooksServlet extends BaseServlet {
	public static final String SUCCCESS="books.action?methodName=doListBooks";
	private BooksService booksService;
	public BooksServlet() {
		booksService=(BooksService) PropertiesFactory.getInstance(Util.BOOKSSERVICE);
	}
	public String doListBooks(HttpServletRequest request ,HttpServletResponse response) {
		String forward="books/listBooks";
		BooksVo booksVo=new BooksVo();
		
		booksVo.setTypeid(Util.parseInt(request.getParameter("typeid")));
		booksVo.setBookname(request.getParameter("bookname"));
		request.setAttribute("blist", this.booksService.queryAllBooks(booksVo));
		request.setAttribute("tlist", this.booksService.queryTypeToBooks());
		return forward;
	}
	public String toAddBooks(HttpServletRequest request, HttpServletResponse response) {
		String forward="books/addBooks";
		request.setAttribute("tlist", this.booksService.queryTypeToBooks());
		return forward;
		
	}
	public String doAddBooks(HttpServletRequest request,HttpServletResponse response) {
		String forward=SUCCCESS;
		request.setAttribute("rows", this.booksService.addBooks(MyBeanUtils.populate(Books.class, request.getParameterMap())));
		return forward;
	}
	public String detailBooks(HttpServletRequest request,HttpServletResponse response) {
		String forward="books/detailBooks";
		request.setAttribute("book", this.booksService.detailBooks(Util.parseInt(request.getParameter("bookid"))));
		return forward;
	}
	public String toUpdateBooks(HttpServletRequest request, HttpServletResponse response) {
		String forward="books/updateBooks";
		request.setAttribute("tlist", this.booksService.queryTypeToBooks());
		request.setAttribute("book", this.booksService.detailBooks(Util.parseInt(request.getParameter("bookid"))));
		return forward;
	}
	public String doUpdateBooks(HttpServletRequest request, HttpServletResponse response) {
		String forward=SUCCCESS;
		Map<String, String[]> param=request.getParameterMap();
		Books books=MyBeanUtils.populate(Books.class, param);
		int rows=this.booksService.updateBooks(books);
		request.setAttribute("rows", rows);
		return forward;
	}
	public String doDeleteBooks(HttpServletRequest request, HttpServletResponse  response) {
		String forward=SUCCCESS;
		int rows=this.booksService.deleteBooks(Util.parseInt(request.getParameter("bookid")));
		request.setAttribute("rows", rows);
		return forward;
	}
}
